package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.enqualcomm.kids.R;

import common.utils.PromptUtil;

/**
 * Created by yinzhiqun on 2015/10/17.
 *
 */
public class ChooseHeadImageDialog extends Dialog {
    private Listener listener;

    public ChooseHeadImageDialog(Context context,Listener listener) {
        super(context);
        this.listener = listener;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.mybuttondialog);
        setDialogAttributes();

        initDialogView();
    }

    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels ;
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public void initDialogView() {
        findViewById(R.id.button_album).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    String state = Environment.getExternalStorageState();
                    if (state.equals(Environment.MEDIA_MOUNTED)) {
                        if (listener != null) {
                            listener.getImageFormAlbum();
                        }
                    } else {
                        PromptUtil.toast(getContext(),R.string.check_sdcard);
                    }
                    dismiss();
                }
                return false;
            }
        });

        findViewById(R.id.button_camera).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    String state = Environment.getExternalStorageState();
                    if (state.equals(Environment.MEDIA_MOUNTED)) {
                        if (listener != null) {
                            listener.getImgaeFromCamera();
                        }
                    } else {
                        PromptUtil.toast(getContext(), R.string.check_sdcard);
                    }

                    dismiss();
                }
                return false;
            }
        });
        // 取消按钮
        findViewById(R.id.cancel_btn).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });
    }

    public interface Listener{
        void getImgaeFromCamera();
        void getImageFormAlbum();
    }

}
